package com.example.apple.ddhr_new.activity.home;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.adapter.BaseFragmentAdapter;

import com.example.lzbase.BaseActivity;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

abstract public class PageActivity extends BaseActivity {

    @BindView(R.id.magic_indicator)
    public MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private BaseFragmentAdapter adapter;


    @Override
    protected int titleText() {
        return R.string.app_name;
    }

    public  abstract  String[] titles();
    public  abstract ArrayList<Fragment> fragments();
    @Override
    protected void created(@Nullable Bundle savedInstanceState) {


        adapter = new BaseFragmentAdapter(getSupportFragmentManager(),fragments());
        viewPager.setAdapter(adapter);

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return titles() == null ? 0 : titles().length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ColorTransitionPagerTitleView colorTransitionPagerTitleView = new ColorTransitionPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(getResources().getColor( R.color.color_cell_title));
                colorTransitionPagerTitleView.setSelectedColor(getResources().getColor( R.color.color_orange));
                colorTransitionPagerTitleView.setText(titles()[index]);
                colorTransitionPagerTitleView.setTextSize(16);
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewPager.setCurrentItem(index);
                        positionSelected(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(getResources().getColor( R.color.color_orange));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
                positionSelected(position);
            }

            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
    }

    public void  positionSelected(int position){

    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_approval;
    }
}
